package com.cortex.pineapple.utilities;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.cortex.pineapple.fetch.RequestData.CATEGORY_FILTER_TYPE;
import com.cortex.pineapple.fetch.RequestData.FILTER_TYPE;
import com.cortex.pineapple.fetch.RequestData;
import com.cortex.pineapple.fetch.ResponseData;
import com.cortex.pineapple.fetch.RequestData.SOURCE_TYPE;
import com.cortex.pineapple.fetch.RequestData.FILTER_TYPE.FILTER_VALUE;
import com.cortext.pineapple.link.ImageLink;
import com.cortext.pineapple.link.Link;
import com.cortext.pineapple.link.NewsLink;
import com.cortext.pineapple.link.VideoLink;
import com.cortext.pineapple.link.WebLink;

public class TestFetchResults {

	public static ResponseData runTesting(RequestData request){
		String json;
		JSONArray results;
		List<WebLink> webLinks;
		List<VideoLink> videoLinks;
		List<NewsLink> newsLinks;
		List<ImageLink> imageLinks;
		
		String query = request.getQuery().replace(" ", "%20");
		CATEGORY_FILTER_TYPE categoryFilterType = request.getCategoryFilterType();
		List<SOURCE_TYPE> sources = request.getSourcesType();
		int top = request.getTop();
		int skip = request.getSkip();
		HashMap<FILTER_TYPE, FILTER_VALUE> filters = request.getFilters();
		
		boolean isSuccess = true;
		try {
			switch(categoryFilterType){
			case IMAGES:
				json = ParseJson.readFromFile("bing image results.txt");
				results = ParseJson.parseResults(json);
				imageLinks = ParseJson.parseImageLinks(results);
				return new ResponseData(isSuccess,
						new ArrayList<WebLink>(),
						imageLinks,
						new ArrayList<VideoLink>(),
						new ArrayList<NewsLink>(),
						query,categoryFilterType,filters,sources);
			case NEWS:
				json = ParseJson.readFromFile("bing news results.txt");
				results = ParseJson.parseResults(json);
				newsLinks = ParseJson.parseNewsLinks(results);
				return new ResponseData(isSuccess,
						new ArrayList<WebLink>(),
						new ArrayList<ImageLink>(),
						new ArrayList<VideoLink>(),
						newsLinks,
						query,categoryFilterType,filters,sources);
			case VIDEO:
				videoLinks = new ArrayList<VideoLink>();
				json = ParseJson.readFromFile("bing video results.txt");
				results = ParseJson.parseResults(json);
				videoLinks = ParseJson.parseVideoLinks(results);
				return new ResponseData(isSuccess,
						new ArrayList<WebLink>(),
						new ArrayList<ImageLink>(),
						videoLinks,new ArrayList<NewsLink>(),
						query,categoryFilterType,filters,sources);
			case WEB:
				json = ParseJson.readFromFile("bing web results.txt");
				results = ParseJson.parseResults(json);
				webLinks = ParseJson.parseWebLinks(results);
				return new ResponseData(isSuccess,
						webLinks,
						new ArrayList<ImageLink>(),
						new ArrayList<VideoLink>(),
						new ArrayList<NewsLink>(),
						query,categoryFilterType,filters,sources);
			case COMPOSITE:
				json = ParseJson.readFromFile("bing composite results.txt");
				results = ParseJson.parseResults(json);
				JSONArray webResults,imageResults,videoResults,newsResults;
				JSONObject result = results.getJSONObject(0);
				
				webResults = result.getJSONArray("Web");
				imageResults = result.getJSONArray("Image");
				videoResults = result.getJSONArray("Video");
				newsResults = result.getJSONArray("News");
				
				webLinks = ParseJson.parseWebLinks(webResults);
				imageLinks = ParseJson.parseImageLinks(imageResults);
				videoLinks = ParseJson.parseVideoLinks(videoResults);
				newsLinks = ParseJson.parseNewsLinks(newsResults);
				return new ResponseData(isSuccess,webLinks,imageLinks,videoLinks,
						newsLinks,query,categoryFilterType,filters,sources);
			default:
				return null;
			}

		} catch (IOException e) {
			Log.d(MessagesLog.APPLICATION_EXCEPTION,"throw exception when read from file " + e.getMessage());
		} catch (JSONException e) {
			Log.d(MessagesLog.APPLICATION_EXCEPTION,"throw exception when parse results " + e.getMessage());
		}
		return null;
	}

}
